package com.novartis.nims.imagedc.util;

import java.text.ParseException;

import javax.swing.text.DefaultFormatter;

@SuppressWarnings("serial")
public class RegexPatternFormatter extends DefaultFormatter {

	  private java.util.regex.Matcher matcher;
	  
	  public RegexPatternFormatter(java.util.regex.Pattern regex) {
		  setOverwriteMode(false);
		  matcher = regex.matcher("");
	  }

	  public Object stringToValue(String string) throws ParseException {
		  if (string == null) {
			  return null;
		  }
		  matcher.reset(string); // set 'string' as the matcher's input

		  if (!matcher.matches()) { // Does 'string' match the regular expression?
			  throw new ParseException("does not match regex", 0);
		  }

		  // If we get this far, then it did match.
		  return super.stringToValue(string);
	  }

}
